//
//  MessageCenterViewController.m
//  Article
//
//  Created by Haricot_cpc17 on 16/4/26.
//  Copyright © 2016年 mac. All rights reserved.
//
//消息中心
#import "MessageCenterViewController.h"
#import "MessageCenterTableViewCell.h"
#import "MessageModel.h"


@interface MessageCenterViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataArray;

@end

@implementation MessageCenterViewController{
    __block int page;

}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    self.tabBarController.tabBar.hidden = YES;
    self.tabBarController.tabBar.translucent = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _dataArray = [NSMutableArray array];
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    [user setObject:@"NO" forKey:@"isMessage"];
    [user synchronize];
    
    [self createUI];
    
    self.navigationItem.title = @"消息中心";
    
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSFontAttributeName:[UIFont fontWithName:@"Heiti SC" size:18],
       NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate =(id)self;
    
    [self.navigationController.navigationBar setBarTintColor:UIColorFromRGB(MYColor)];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"arrow"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    [self requestTableview];
}

- (void)requestTableview{
    if (_dataArray == nil || page == 1) {
        _dataArray = [NSMutableArray arrayWithCapacity:0];;
        
    }
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    
    NSDictionary *dic = @{@"user_token":[user objectForKey:@"token"],
                          @"page":[NSString stringWithFormat:@"%d",page]};
    AFHTTPRequestOperationManager *manager = [MyTools initAFHttpManager];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [manager POST:MESSAGE_URL parameters:dic success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        if ([[responseObject objectForKey:@"status"] intValue] == 1) {
            //建立模型
            for (NSDictionary*dic in [responseObject objectForKey:@"data"]) {
                MessageModel *model=[[MessageModel alloc]init];
                
                [model setValuesForKeysWithDictionary:dic];
                
                //追加数据
                [_dataArray addObject:model];
            }
            
            
        }else{
            [self.view makeToast:[responseObject objectForKey:@"info"] duration:1.0 position:CSToastPositionCenter];
        }
        if (_dataArray.count > 8) {
            [_tableView addGifFooterWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
        }
        [_tableView reloadData];
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        
        if ([MyTools connectedToNetwork] == 0) {
            [self.view makeToast:@"请检查是否联网" duration:1.0 position:CSToastPositionCenter];
        }else{
            [self.view makeToast:@"服务器错误，再试一次" duration:1.0 position:CSToastPositionCenter];
        }
    }];
}
//下拉刷新
-(void)downMJ{
    page = 1;
    [self requestTableview];
    
    [_tableView.header endRefreshing];
}
//上拉加载
- (void)loadMoreData{
    page++;
    [self requestTableview];
    [_tableView.footer endRefreshing];
    
}

-(void)back{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)createUI{
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 49) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView addGifHeaderWithRefreshingTarget:self refreshingAction:@selector(downMJ)];

    [_tableView registerNib:[UINib nibWithNibName:@"MessageCenterTableViewCell" bundle:nil] forCellReuseIdentifier:@"messageCenterID"];
    [self.view addSubview:_tableView];
    [self setExtraCellLineHidden:_tableView];
    _tableView.showsVerticalScrollIndicator = NO;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MessageCenterTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"messageCenterID" forIndexPath:indexPath];
    MessageModel*model = _dataArray[indexPath.row];
    [cell configModel:model];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [_tableView deselectRowAtIndexPath:indexPath animated:NO];
}
-(void)setExtraCellLineHidden: (UITableView *)tableView
{
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor clearColor];
    [tableView setTableFooterView:view];
}


@end
